Imports System.Configuration
Module modMain

#Region "Variables"

    ' Company Default Structure
    Public Structure CompanyDefault
        Public sDBName As String
        Public sServer As String
        Public sSAPUser As String
        Public sSAPPwd As String
        Public sSAPDBName As String
        Public sDBUser As String
        Public sDBPwd As String

        Public sEmailFrom As String
        Public sEmailTo As String
        Public sEmailSubject As String
        Public sSMTPServer As String
        Public sSMTPPort As String
        Public sSMTPUser As String
        Public sSMTPPassword As String

        Public sReportsPath As String
        Public sDailyReportName As String
        Public sWeeklyReportName As String
        Public sWeeklyDay As String

    End Structure

    ' Return Value Variable Control
    Public Const RTN_SUCCESS As Int16 = 1
    Public Const RTN_ERROR As Int16 = 0
    ' Debug Value Variable Control
    Public Const DEBUG_ON As Int16 = 1
    Public Const DEBUG_OFF As Int16 = 0

    ' Global variables group
    Public p_iDebugMode As Int16 = DEBUG_ON
    Public p_iErrDispMethod As Int16
    Public p_iDeleteDebugLog As Int16
    Public p_oCompDef As CompanyDefault
    Public p_SyncDateTime As String

#End Region

    Sub Main()

        Dim sFuncName As String = String.Empty
        Dim sErrDesc As String = String.Empty
        Dim AttachmentsDirInfo As System.IO.DirectoryInfo
        Dim AttachmentsFileInfo As System.IO.FileInfo() = Nothing
        Dim sAttachmentPath As String = System.IO.Directory.GetCurrentDirectory() & "\ReportsPDF"
        Dim File As System.IO.FileInfo

        Try

            sFuncName = "Apex - EmailService"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Function", sFuncName)

            Console.Title = "Apex - EmailService"

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling GetSystemIntializeInfo()", sFuncName)
            If GetSystemIntializeInfo(p_oCompDef, sErrDesc) <> RTN_SUCCESS Then Throw New ArgumentException(sErrDesc)

            'Delete Previous day PDF files
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Delete Previous day PDF files..()", sFuncName)
            If Not String.IsNullOrEmpty(sAttachmentPath) Then
                If System.IO.Directory.Exists(sAttachmentPath) Then
                    AttachmentsDirInfo = New System.IO.DirectoryInfo(sAttachmentPath)
                    AttachmentsFileInfo = AttachmentsDirInfo.GetFiles("*.pdf*")
                End If
            End If

            For Each File In AttachmentsFileInfo
                File.Delete()
            Next

            'Daily Report
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Export - Daily Report()", sFuncName)
            Console.WriteLine("Export to pdf - Daily Report Name::" & p_oCompDef.sDailyReportName)
            If ExportToPDF(p_oCompDef.sDailyReportName, sErrDesc) <> RTN_SUCCESS Then
                Throw New ArgumentException(sErrDesc)
            End If
            Console.WriteLine("Completed")

            'Weekly Report
            If Now.DayOfWeek = CInt(p_oCompDef.sWeeklyDay) Then
                Console.Title = "Export to pdf - Weekly Report Name::" & p_oCompDef.sDailyReportName
                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Export - Weekly Report()", sFuncName)
                If ExportToPDF(p_oCompDef.sWeeklyReportName, sErrDesc) <> RTN_SUCCESS Then
                    Throw New ArgumentException(sErrDesc)
                End If
                Console.WriteLine("Completed")
            End If

            Console.WriteLine("Sending Email...")
            If SendEmail(sErrDesc) <> RTN_SUCCESS Then
                Throw New ArgumentException(sErrDesc)
            End If

            Console.WriteLine("Successfully sent email.")

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
        Catch ex As Exception
            WriteToLogFile(ex.Message, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            End
        End Try
    End Sub

    Public Function GetSystemIntializeInfo(ByRef oCompDef As CompanyDefault, ByRef sErrDesc As String) As Long

        ' **********************************************************************************
        '   Function    :   GetSystemIntializeInfo()
        '   Purpose     :   This function will be providing to proceed the initializing 
        '                   variable control during the system start-up
        '               
        '   Parameters  :   ByRef oCompDef As CompanyDefault
        '                       oCompDef =  set the Company Default structure
        '                   ByRef sErrDesc AS String 
        '                       sErrDesc = Error Description to be returned to calling function
        '               
        '   Return      :   0 - FAILURE
        '                   1 - SUCCESS
        '   Author      :   SRI
        '   Date        :   Jan 2014
        ' **********************************************************************************

        Dim sFuncName As String = String.Empty
        Dim sConnection As String = String.Empty
        Try

            sFuncName = "GetSystemIntializeInfo()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Function", sFuncName)

            oCompDef.sDBName = String.Empty
            oCompDef.sServer = String.Empty
            oCompDef.sSAPUser = String.Empty
            oCompDef.sSAPPwd = String.Empty
            oCompDef.sSAPDBName = String.Empty


            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("Server")) Then
                oCompDef.sServer = ConfigurationManager.AppSettings("Server")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("SAPDBName")) Then
                oCompDef.sSAPDBName = ConfigurationManager.AppSettings("SAPDBName")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("SAPUserName")) Then
                oCompDef.sSAPUser = ConfigurationManager.AppSettings("SAPUserName")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("SAPPassword")) Then
                oCompDef.sSAPPwd = ConfigurationManager.AppSettings("SAPPassword")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("DBUser")) Then
                oCompDef.sDBUser = ConfigurationManager.AppSettings("DBUser")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("DBPwd")) Then
                oCompDef.sDBPwd = ConfigurationManager.AppSettings("DBPwd")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("EmailFrom")) Then
                oCompDef.sEmailFrom = ConfigurationManager.AppSettings("EmailFrom")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("EmailTo")) Then
                oCompDef.sEmailTo = ConfigurationManager.AppSettings("EmailTo")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("EmailSubject")) Then
                oCompDef.sEmailSubject = ConfigurationManager.AppSettings("EmailSubject")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("SMTPServer")) Then
                oCompDef.sSMTPServer = ConfigurationManager.AppSettings("SMTPServer")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("SMTPPort")) Then
                oCompDef.sSMTPPort = ConfigurationManager.AppSettings("SMTPPort")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("SMTPUser")) Then
                oCompDef.sSMTPUser = ConfigurationManager.AppSettings("SMTPUser")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("SMTPPassword")) Then
                oCompDef.sSMTPPassword = ConfigurationManager.AppSettings("SMTPPassword")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("DailyReportName")) Then
                oCompDef.sDailyReportName = ConfigurationManager.AppSettings("DailyReportName")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("WeeklyReportName")) Then
                oCompDef.sWeeklyReportName = ConfigurationManager.AppSettings("WeeklyReportName")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("ReportsPath")) Then
                oCompDef.sReportsPath = ConfigurationManager.AppSettings("ReportsPath")
            End If

            If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("WeeklyDay")) Then
                oCompDef.sWeeklyDay = ConfigurationManager.AppSettings("WeeklyDay")
            End If

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
            GetSystemIntializeInfo = RTN_SUCCESS

        Catch ex As Exception
            WriteToLogFile(ex.Message, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            GetSystemIntializeInfo = RTN_ERROR
        End Try
    End Function

End Module
